/**
 * Created by linweihao on 15/12/23.
 */
var Promise = require('bluebird')
    , Request = Promise.promisify(require("request"))
    , _ = require('lodash')

function RequestHexun(name) {
    var url = "http://webstock.quote.hermes.hexun.com/a/kline?code=szse000009&start=20151223150000&number=-100000&type=5"
    return new Promise(function (resolve, reject) {
        Request.get({url: url}, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                resolve(body)
            } else {
                reject(err)
            }
        })
    })
}

function averageHexun(arr, averageDays, days) {
    var count = arr.length
        , num = averageDays || 5
    if (days) {
        arr = _.takeRight(arr, days)
    }
    arr.forEach(function (obj, i) {
        if (i >= num - 1) {
            var sum = 0
            for (var j = i; (i - j) < num; j--) {
                sum = sum + parseFloat(arr[j][3])
            }
            var average = sum / num
            obj[8] = average
        }
    })
    return arr
}

function calculateHexun(arr, isRise) {
    var rise = false, recordPosition, count = 0, total = 0, startCaluate = false, endCaluate = false, result = {
        count: 0,
        total: 0,
        days: 0
    }
    arr.forEach(function (obj, i) {
        if (obj[8]) {
            var differ = obj[3] - obj[8]
            console.log('diff....' + differ + "....i...." + i)
            if (isRise) {
                rise = (differ > 0 ) ? true : false
            } else {
                rise = (differ < 0 ) ? true : false
            }

            if (rise) {
                if (!startCaluate) {
                    startCaluate = true
                    recordPosition = i
                }
            } else {
                if (startCaluate && !endCaluate) {
                    endCaluate = true
                }
            }

            if (startCaluate && endCaluate) {
                count = count + 1
                var start = arr[recordPosition + 1][2] + 1
                    , endPositon = (i + 1) >= arr.length ? true : false
                if (endPositon) {
                    return
                }
                var end = arr[i + 1][2] - 1
                    , income = end - start
                console.log('recordPosition....' + recordPosition + "...i..." + i + '...income..' + income)
                total = total + income
                startCaluate = false
                endCaluate = false
            }
        }
    })
    result.days = arr.length
    result.count = count
    result.total = total
    if (!isRise) {
        result.total = -total
    }
    return result
}

Promise.resolve()
    .then(function () {
        return RequestHexun()
    }).then(function (result) {
        result = result.replace('(', '')
        result = result.replace(');', '')
        result = JSON.parse(result)
        console.log(result.KLine)
        console.log(result.Data[0].length)
        return averageHexun(result.Data[0], 5, 100)
    }).then(function (a) {
        return calculateHexun(a, true)
    }).then(function (result) {
        console.log(result)
    })